package org.abratuhi.kvngroupware.widget;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.abratuhi.kvngroupware.network.MessageSender;

@SuppressWarnings("serial")
public abstract class AbstractWidget extends JPanel{

	protected MessageSender sender;
	
	protected String frametitle;
	protected JFrame frame;
	
	public AbstractWidget(String frametitle, MessageSender sender){
		super();
		
		this.frametitle = frametitle;
		this.sender = sender;
		
		setLayout(new BorderLayout());
		setPreferredSize(new Dimension(200,200));
		setVisible(true);
	}
	
	public JFrame getWindow(){
		if(frame==null){
			createWindow();
		}
		return frame;
	}
	
	public boolean existsWindow(){
		return frame==null;
	}
	
	public void createWindow(){
		frame = new JFrame(frametitle);
		frame.setLayout(new BorderLayout());
		frame.add(this);
		frame.setVisible(true);
		frame.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent we){
				onFrameClose();
			}
		});
		frame.pack();
	}
	
	public void deleteWindow(){
		if(frame!=null){ // TODO: does this check really belong here?
			frame.dispose();
			frame = null;
		}
	}
	
	public abstract void onFrameClose();

}
